package com.doyens.gmall.all.controller;

import com.doyens.gmall.common.result.Result;
import com.doyens.gmall.list.client.ListFeignClient;
import com.doyens.gmall.model.list.SearchParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Classname:ListController
 * PackageName:com.doyens.gmall.all.controller
 *
 * @Author:uyijk
 * @Create:2023/8/3 14:16
 * @Version: 1.0
 */
@Controller
@Slf4j
public class ListController {
    @Autowired
    private ListFeignClient listFeignClient;
    @GetMapping("list.html")
    public String list(SearchParam searchParam, Model model){
        log.info(searchParam.toString());
        Result result = listFeignClient.list(searchParam);
        Map data = (Map) result.getData();
        model.addAllAttributes(data);
        //把查询的条件再传回页面，方便下次继续查询
        String urlParam = makeUrlParam(searchParam);
        model.addAttribute("urlParam",urlParam);
        model.addAttribute("searchParam",searchParam);
        String trademark = makeTrademark(searchParam.getTrademark());
        List<Map<String,String>> props = makeProps(searchParam.getProps());
        model.addAttribute("propsParamList",props);
        model.addAttribute("trademarkParam",trademark);
        Map<String,Object> orderMap = dealOrder(searchParam.getOrder());
        model.addAttribute("orderMap",orderMap);
        return "list/index";
    }

    /**
     *
     * @param order  1:asc  2:desc  1:desc   (1=hotScore   2=price)
     * @return {type:1,sort:asc/desc}
     */
    private Map<String, Object> dealOrder(String order) {
        Map<String, Object> map = new HashMap<>();
        if(!StringUtils.isEmpty(order)){
            String[] split = order.split(":");
            if(split!=null && split.length==2){
                map.put("type",split[0]);
                map.put("sort",split[1]);
            }
        }else {
            map.put("type","1");
            map.put("sort","asc");
        }
        return map;
    }

    /**
     *
     * @param props 23:4G:运行内存  24:5G:运行内存
     * @return  [{attrid:a,attrName:b,attrValue:c},{attrid:a,attrName:b,attrValue:c}]
     */
    private List<Map<String, String>> makeProps(String[] props) {
        List<Map<String,String>> list = new ArrayList<>();
        if(props!=null && props.length>0){
            for (String prop : props) {
                //prop=23:4G:运行内存
                if(prop!=null){
                    String[] split = prop.split(":");
                    if(split!=null && split.length==3){
                        Map<String,String> map = new HashMap<>();
                        map.put("attrId",split[0]);
                        map.put("attrValue",split[1]);
                        map.put("attrName",split[2]);
                        list.add(map);
                    }
                }
            }
        }
        return list;
    }

    /***
     *
     * @param trademark 格式为  2:华为
     * @return  品牌:华为
     */
    private String makeTrademark(String trademark) {
        if(!StringUtils.isEmpty(trademark)){
            String[] split = trademark.split(":");
            if(split!=null && split.length==2){
                return "品牌:"+split[1];
            }
        }
        return "";
    }

    private String makeUrlParam(SearchParam searchParam) {
        StringBuilder stringBuilder = new StringBuilder();
        if(searchParam.getKeyword()!=null){
            stringBuilder.append("keyword="+searchParam.getKeyword());
        }
        if(searchParam.getCategory1Id()!=null){
            stringBuilder.append("category1Id="+searchParam.getCategory1Id());
        }
        if(searchParam.getCategory2Id()!=null){
            stringBuilder.append("category2Id="+searchParam.getCategory2Id());
        }
        if(searchParam.getCategory3Id()!=null){
            stringBuilder.append("category3Id="+searchParam.getCategory3Id());
        }

        if(searchParam.getTrademark()!=null){
            stringBuilder.append("&trademark="+searchParam.getTrademark());
        }

        if(searchParam.getProps()!=null&&searchParam.getProps().length>0){
            for (String prop : searchParam.getProps()) {
                stringBuilder.append("&props="+prop);
            }
        }
        return "list.html?"+stringBuilder.toString();

    }

}
